<template>
  <div class="rollContainer">
    <div class="searchHead" ref="searchHead">
      <Form ref="formInline" :model="conditions" inline :label-width="120" :show-message="false">
        <div class="searchBox">
          <div class="conditionBox">
            <Row>
              <!-- <Col :span="6">
                <FormItem label="展会选择：">
                  <Select
                    v-model="conditions.app_type"
                    clearable
                    @on-change="changeStatus($event, 'app_type')"
                  >
                    <Option
                      v-for="item in client"
                      :value="item.id"
                      :key="item.value"
                    >{{ item.title }}</Option>
                  </Select>
                </FormItem>
              </Col>-->
              <!-- <Col :span="6">
                <FormItem label="选择类型：">
                  <Select v-model="conditions.by" clearable @on-change="chooseType($event, 'by')">
                    <Option
                      v-for="item in statusList"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
              </Col>-->
              <Col :span="6">
                <FormItem label="合作伙伴分类：">
                  <Select
                    v-model="conditions.typeId"
                    clearable
                    @on-change="changeStatus($event, 'typeId')"
                  >
                    <Option
                      v-for="item in typeLists"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
          </div>
          <div class="searchBtnBox">
            <Button type="primary" @click="search()">搜索</Button>
            <Button @click="handleReset()">重置</Button>
          </div>
        </div>
      </Form>
      <div class="tableBtnBox">
        <Button icon="md-add" size="default" @click="Publish">新增合作伙伴</Button>
      </div>
    </div>
    <Table :columns="columns1" :height="tableHeight" border :data="list" :loading="loading">
      <template slot-scope="{ row }" slot="createTime">
        <span>{{row.createTime|dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
      </template>
      <template slot-scope="{ row }" slot="updateTime">
        <span>{{row.updateTime|dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
      </template>
      <template slot-scope="{ row }" slot="action">
        <span class="tableBtn" @click="show(row)">
          <Icon size="18" type="ios-create-outline" />编辑
        </span>
        <span class="tableBtn" @click="remove(row.friendId)">
          <Icon size="18" type="ios-trash" />删除
        </span>
      </template>
    </Table>
    <div class="pageBox">
      <Page
        :total="total"
        :page-size="pageSize"
        :current="page"
        size="small"
        show-total
        show-sizer
        @on-change="changePage"
        @on-page-size-change="changeSize"
      />
    </div>
    <Modal v-model="isShow" title="活动伙伴新增/编辑" width="1000">
      <Form
        ref="formInline"
        :model="formInline"
        :rules="ruleInline"
        inline
        :label-width="100"
        fullscreen
      >
        <FormItem prop="name" label="名称">
          <Input type="text" v-model.trim="formInline.name" placeholder="请输入名称" style="width：300px"></Input>
        </FormItem>
        <FormItem prop="url" label="链接">
          <Input type="text" v-model.trim="formInline.url" placeholder="请输入链接" style="width：300px"></Input>
        </FormItem>
        <!-- <FormItem prop="content" label="文章内容">
          <Input type="text" v-model.trim="formInline.content" type="textarea" placeholder="请输入文章内容" style="width：300px"></Input>
        </FormItem>-->
        <FormItem label="合作分类：" prop="typeId">
          <Select v-model="formInline.typeId" clearable @on-change="checkStatus($event, 'typeId')">
            <Option v-for="item in typeLists" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </FormItem>
        <FormItem prop="typeName" label="新分类名称">
          <Input
            type="text"
            v-model.trim="formInline.typeName"
            placeholder="请输入新分类名称"
            style="width：300px"
          ></Input>
        </FormItem>
        <FormItem prop="image" label="LOGO">
          <upload
            limit="1"
            @uploadImg="uploadImg($event,'image',multiple=false)"
            :imgs="formInline.image"
          ></upload>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="isShow=false">取消</Button>
        <Button type="primary" @click="sure('formInline')">确认</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import {
  friendsList,
  addfriends,
  modifyfriends,
  delfriends,
  typeList
} from "../../api/partners";
import upload from "@/components/upload.vue";
export default {
  components: {
    upload
  },
  data() {
    return {
      limit: 1, //允许上传的数量
      typeLists: [], //分类列表
      list: [],
      page: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      conditions: {
        typeId: ""
      },
      columns1: [
        {
          title: "id",
          key: "friendId",
          align: "center",
          minWidth: 200
        },
        {
          title: "名称",
          key: "name",
          align: "center",
          minWidth: 200
        },
        {
          title: "分类名称",
          key: "typeName",
          align: "center",
          minWidth: 200
        },
        {
          title: "网址",
          key: "url",
          align: "center",
          minWidth: 200
        },
        {
          title: "创建时间",
          slot: "createTime",
          align: "center",
          minWidth: 200
        },
        {
          title: "更新时间",
          slot: "updateTime",
          align: "center",
          minWidth: 200
        },
        {
          title: "操作",
          slot: "action",
          width: 200,
          align: "center"
        }
      ],
      tableHeight: 100,
      isShow: false,
      isFixed: false,
      username: "",
      id: "", //id
      formInline: {
        name: "",
        url: "",
        typeId: "",
        image: "",
        typeName: ""
      },
      ruleInline: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ],
        image: [
          {
            required: true,
            message: "请上传LOGO",
            trigger: "change"
          }
        ],
        // typeId: [
        //   { required: true, message: "请选择合作分类", trigger: "change" }
        // ]
      }
    };
  },
  created() {
    this.gettypeList();
    this.getfriendsList();
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight =
        window.innerHeight - this.$refs.searchHead.offsetHeight - 220;
    });
  },
  methods: {
    // 上传文件
    uploadImg(imgs, k, multiple) {
      if (multiple) {
        this.formInline[k] = JSON.parse(imgs);
      } else {
        this.formInline[k] = JSON.parse(imgs)[0];
      }
    },
    checkStatus(e, name) {
      this.formInline[name] = e;
    },
    changeStatus(e, name) {
      this.page = 1;
      this.conditions[name] = e;
      this.getfriendsList();
    },
    gettypeList() {
      //分类列表
      typeList().then(res => {
        if (res.status) {
          this.typeLists = res.data.map(e => {
            return {
              value: e.id,
              label: e.name
            };
          });
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    getfriendsList() {
      //合作列表
      this.loading = true;
      const { page, pageSize } = this;
      friendsList(
        Object.assign(this.conditions, { page: page - 1, pageSize })
      ).then(res => {
        if (res.status) {
          this.total = res.data.count;
          this.list = res.data.list;
          this.loading = false;
        } else {
          this.$Message.error(res.msg);
          this.loading = false;
        }
      });
    },
    show(row) {
      this.id = row.friendId;
      this.formInline = {
        name: row.name,
        url: row.url,
        typeId: row.typeId,
        image: row.image,
        typeName: ""
      };
      this.isShow = true;
    },
    changePage(e) {
      this.page = e;
      this.getfriendsList();
    },
    changeSize(e) {
      this.page = 1;
      this.pageSize = e;
      this.getfriendsList();
    },
    Publish() {
      this.id = "";
      this.formInline = {
        name: "",
        url: "",
        typeId: "",
        // content: "",
        image: "",
        typeName: ""
      };
      this.isShow = true;
    },
    sure(nam) {
      const { name, url, typeId, image, typeName } = this.formInline;
      this.$refs[nam].validate(valid => {
        if (valid) {
          if (this.id == "") {
            addfriends({
              name,
              url,
              typeId: typeId,
              image,
              typeName
            }).then(res => {
              console.log(res);
              if (res.status) {
                this.isShow = false;
                this.getfriendsList();
                this.$Message.success(res.msg);
              } else {
                this.$Message.error(res.msg);
              }
            });
          } else {
            modifyfriends({
              friendId: this.id,
              name,
              url,
              typeId: typeId,
              image,
              typeName
            }).then(res => {
              if (res.status) {
                this.isShow = false;
                this.getfriendsList();
                this.$Message.success(res.msg);
              } else {
                this.$Message.error(res.msg);
              }
            });
          }
        } else {
        }
      });
    },
    //删除合作伙伴
    remove(friendId) {
      this.$Modal.confirm({
        title: "警告",
        content: `删除后无法恢复，确定要删除？`,
        onOk: () => {
          delfriends({ friendId }).then(res => {
            if (res.status) {
              this.getfriendsList();
              this.$Message.success("删除成功");
            } else {
              this.$Message.error(res.msg);
            }
          });
        },
        onCancel: () => {}
      });
    },
    cancel() {
      this.isShow = false;
    },
    editorChange(e) {},
    search() {
      this.getfriendsList();
    },
    handleReset() {
      this.conditions = {
        typeId: ""
      };
      this.page = 1;
      this.getfriendsList();
    }
  }
};
</script>
<style lang="less" scoped>
.page {
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}
</style>
